package com.joolun.web.controller.mall;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.joolun.common.annotation.Log;
import com.joolun.common.core.controller.BaseController;
import com.joolun.common.core.domain.AjaxResult;
import com.joolun.common.enums.BusinessType;
import com.joolun.common.utils.poi.ExcelUtil;
import com.joolun.mall.domain.vo.ChainPlanReserveOrderError;
import com.joolun.mall.entity.ChainPlanReserveOrder;
import com.joolun.mall.service.IChainPlanReserveOrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
import java.util.Objects;

/**
 * 接龙计划预留名单Controller
 * 
 * @author www.joolun.com
 * @date 2025-08-04
 */
@RestController
@RequestMapping("/chainPlan/reserveorder")
public class ChainPlanReserveOrderController extends BaseController
{
    @Autowired
    private IChainPlanReserveOrderService chainPlanReserveOrderService;

    /**
     * 查询接龙计划预留名单列表
     */
//    @PreAuthorize("@ss.hasPermi('mall:reserveorder:list')")
    @Log(title = "接龙计划预留名单列表", businessType = BusinessType.QUERY)
    @GetMapping("/list")
    public AjaxResult<Page<ChainPlanReserveOrder>> list(Page<ChainPlanReserveOrder> page, ChainPlanReserveOrder chainPlanReserveOrder)
    {
        Page<ChainPlanReserveOrder> page1 = chainPlanReserveOrderService.page(page,
                Wrappers.<ChainPlanReserveOrder>lambdaQuery()
                .like(ChainPlanReserveOrder::getPlanId, chainPlanReserveOrder.getPlanId())
                .like(Objects.nonNull(chainPlanReserveOrder.getTbUserId()),ChainPlanReserveOrder::getTbUserId, chainPlanReserveOrder.getTbUserId())
                .like(Objects.nonNull(chainPlanReserveOrder.getWdtEncodeUserId()),ChainPlanReserveOrder::getWdtEncodeUserId, chainPlanReserveOrder.getWdtEncodeUserId())
                .like(Objects.nonNull(chainPlanReserveOrder.getSkuCode()),ChainPlanReserveOrder::getSkuCode, chainPlanReserveOrder.getSkuCode())
                .in(ChainPlanReserveOrder::getDelFlag, "0")
                .orderByDesc(ChainPlanReserveOrder::getCreateTime)
        );

        return AjaxResult.success(page1);
    }

    /**
     * 导出接龙计划预留名单列表
     */
//    @PreAuthorize("@ss.hasPermi('mall:reserveorder:export')")
    @Log(title = "接龙计划预留名单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(ChainPlanReserveOrder chainPlanReserveOrder)
    {
        List<ChainPlanReserveOrder> list = chainPlanReserveOrderService.selectChainPlanReserveOrderList(chainPlanReserveOrder);
        ExcelUtil<ChainPlanReserveOrder> util = new ExcelUtil<ChainPlanReserveOrder>(ChainPlanReserveOrder.class);
        return util.exportExcel(list, "发货订单信息");
    }

    /**
     * 获取接龙计划预留名单详细信息
     */
//    @PreAuthorize("@ss.hasPermi('mall:reserveorder:query')")
    @Log(title = "接龙计划预留名单详细信息", businessType = BusinessType.QUERY)
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(chainPlanReserveOrderService.selectChainPlanReserveOrderById(id));
    }

    /**
     * 新增接龙计划预留名单
     */
//    @PreAuthorize("@ss.hasPermi('mall:reserveorder:add')")

    @Log(title = "接龙计划预留名单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ChainPlanReserveOrder chainPlanReserveOrder)
    {
        return AjaxResult.success(chainPlanReserveOrderService.insertChainPlanReserveOrder(chainPlanReserveOrder,null));

    }

    /**
     * 修改接龙计划预留名单
     */
//    @PreAuthorize("@ss.hasPermi('mall:reserveorder:edit')")
    @Log(title = "接龙计划预留名单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ChainPlanReserveOrder chainPlanReserveOrder)
    {
        return toAjax(chainPlanReserveOrderService.updateChainPlanReserveOrder(chainPlanReserveOrder));
    }

    /**
     * 删除接龙计划预留名单
     */
//    @PreAuthorize("@ss.hasPermi('mall:reserveorder:remove')")
    @Log(title = "接龙计划预留名单", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(chainPlanReserveOrderService.deleteChainPlanReserveOrderByIds(ids));
    }



    /**
     * 导入
     */
//    @PreAuthorize("@ss.hasPermi('mall:reserveorder:remove')")
    @Log(title = "导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(@RequestParam("file")MultipartFile file,@RequestParam("planId") Long planId) throws Exception {
        ExcelUtil<ChainPlanReserveOrder> util = new ExcelUtil<>(ChainPlanReserveOrder.class);
        List<ChainPlanReserveOrder> datas = util.importExcel(file.getInputStream());

        datas.forEach(item -> item.setPlanId(planId));
        List<ChainPlanReserveOrderError> chainPlanReserveOrderErrors = chainPlanReserveOrderService.importData(datas);

        if (!chainPlanReserveOrderErrors.isEmpty()){
            //导出数据
            ExcelUtil<ChainPlanReserveOrderError> util2 = new ExcelUtil<>(ChainPlanReserveOrderError.class);
            return util2.exportExcel(chainPlanReserveOrderErrors, "发货订单信息");
        }else{
            return AjaxResult.success("导入成功");
        }
    }
}
